package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.GjgxAdapter;
import zj.com.app.ZjApplication;
import zj.com.model.DjcModel;
import zj.com.model.Gjgx;
import zj.com.model.GsTj;
import zj.com.model.JcxDB;
import zj.com.model.Org;
import zj.com.model.Project;
import zj.com.model.UserInfo;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/9.
 *
 * 关键工序
 */
public class GjgxActivity extends  BaseActivity implements View.OnClickListener{

    public TextView title_name,right_tv,cancel_tv,submit_tv,del_tv,select_gs_tv,select_prj_tv,select_status_tv;
    public ImageView left_iv;

    public ListView listView;
    public GjgxAdapter adapter;
    public List<Gjgx> list;

    public LinearLayout edit_ll;
    public Intent intent;
    public TextView add_tc;
    public RelativeLayout error_rl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gjgx);
        initView();
        initData();
    }

    public void initView(){

        error_rl = (RelativeLayout) findViewById(R.id.error_rl);
        add_tc = (TextView) findViewById(R.id.add_tc);
        add_tc.setOnClickListener(this);

        select_gs_tv = (TextView) findViewById(R.id.select_gs_tv);
        select_gs_tv.setOnClickListener(this);
        select_prj_tv = (TextView) findViewById(R.id.select_prj_tv);
        select_prj_tv.setOnClickListener(this);
        select_status_tv = (TextView) findViewById(R.id.select_status_tv);
        select_status_tv.setOnClickListener(this);


        cancel_tv = (TextView) findViewById(R.id.cancel_tv);
        cancel_tv.setOnClickListener(this);
        edit_ll = (LinearLayout) findViewById(R.id.edit_ll);
        submit_tv = (TextView) findViewById(R.id.submit_tv);
        submit_tv.setOnClickListener(this);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("关键工序查询");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("编辑");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);

        del_tv = (TextView) findViewById(R.id.del_tv);
        del_tv.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                intent = new Intent(GjgxActivity.this,GjgxEditActivity.class);
                intent.putExtra("gjgx",list.get(position));
                intent.putExtra("isEdit",true);
                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                edit_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);
                return false;
            }
        });
    }

    public void initData(){

       list = new ArrayList<Gjgx>();

        adapter = new GjgxAdapter(list,this);
        listView.setAdapter(adapter);

        obtainGs();
        //obtainData();
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                edit_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);
                break;
            case R.id.cancel_tv:
                edit_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);

                break;
            case R.id.submit_tv:
                edit_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);

                break;
            case R.id.del_tv:
                edit_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                break;
            case R.id.select_gs_tv:

                if(list == null || list.size() == 0) return;
                intent = new Intent(this,SelectDoubleListActivity.class);
                startActivityForResult(intent, GS_XM_CODE);
                break;
            case R.id.select_prj_tv:
                if(list == null || list.size() == 0) return;
                intent = new Intent(this,SelectDoubleListActivity.class);
                startActivityForResult(intent, GS_XM_CODE);
                break;
            case R.id.select_status_tv:
                intent = new Intent(this,SelectActivity.class);
                intent.putExtra("type",6);
                startActivityForResult(intent,STATUS_CODE);
                break;
            case R.id.add_tc:
                intent = new Intent(this,ZlzpActivity.class);
                startActivity(intent);

                break;
        }
    }

    public List<Org> orgList;
    public final int GS_XM_CODE = 0x2210;
    public final int STATUS_CODE = GS_XM_CODE+1;

    public ProgressDialog dialog;
    public void obtainGs(){

        dialog = DialogUtil.createProgressDialog(this,"提示","数据获取中");
        NetManager.getInstance(this).QueryOrgs(new OnResponseListener<List<Org>>() {
            @Override
            public void onComplete(boolean isOK, List<Org> result, int errorCode, String errorMsg) {

                if (isOK) {
                    orgList = result;
                    LogUtils.getInstance().error("======result======"+result.size());
                    if (orgList != null && orgList.size() > 0) {

                        Org org = orgList.get(0);
                        ZjApplication.orgList = orgList;
                        select_gs_tv.setText(org.getOrgName());
                        obtainProject(org);
                    }else{
                        Object obj = SharedPreferencesUtil.readObject(GjgxActivity.this, "user");
                        if(obj != null) {

                          UserInfo  uinfo = (UserInfo) obj;
                            select_gs_tv.setText(uinfo.getOrgFullName());
                            Org org = new Org();
                            org.setOrgCode(uinfo.getOrgCode());
                            org.setOrgName(uinfo.getOrgFullName());
                            obtainProject(org);
                        }

                    }
                } else {
                    Toast.makeText(GjgxActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {

                dialog.dismiss();
                Toast.makeText(GjgxActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void obtainProject(final  Org org){

        LogUtils.getInstance().error("==========obtainProject=============");
        NetManager.getInstance(this).GetProjectList(org.getOrgCode(), new OnResponseListener<List<Project>>() {
            @Override
            public void onComplete(boolean isOK, List<Project> result, int errorCode, String errorMsg) {

                if (isOK) {

                    if(result != null && result.size() > 0){

                        selectProject =  result.get(0);
                        select_prj_tv.setText(selectProject.getProjectName());
                        ZjApplication.projectMap.put(org.getOrgCode(),result);
                        obtainData();
                    }else{
                        dialog.dismiss();

                    }


                } else {
                    Toast.makeText(GjgxActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(GjgxActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }

    public void obtainData(){

        NetManager.getInstance(this).GetCriticalActivity(selectProject.getProjectID()+"", "", ""+status, "", "1", "100", new OnResponseListener<List<Gjgx>>() {
            @Override
            public void onComplete(boolean isOK, List<Gjgx> result, int errorCode, String errorMsg) {
                dialog.dismiss();

                if(isOK){
                    list = result;
                    if(list != null && list.size() > 0){
                        error_rl.setVisibility(View.GONE);
                    }else{
                        error_rl.setVisibility(View.VISIBLE);

                    }
                    adapter.setDataChange(list);
                }else{
                    error_rl.setVisibility(View.VISIBLE);

                    Toast.makeText(GjgxActivity.this,"获取失败",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
                error_rl.setVisibility(View.VISIBLE);
                Toast.makeText(GjgxActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
            }
        });

    }

    public Project selectProject;
    public Org selectOrg;
    public int status = 0;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){

            if(requestCode == GS_XM_CODE){

                selectProject = (Project) data.getSerializableExtra("project");
                selectOrg = (Org) data.getSerializableExtra("org");
                if(selectProject != null){

                    select_prj_tv.setText(selectProject.getProjectName());
                }
                if(selectOrg != null){
                    select_gs_tv.setText(selectOrg.getOrgName());
                }
            }else if(requestCode == STATUS_CODE){


               int position =  data.getIntExtra("position",0);
                LogUtils.getInstance().error("=====STATUS_CODE===="+position);
                status = position;
                if(position == 0){
                    select_status_tv.setText("未开始");

                }else if(position == 1){
                    select_status_tv.setText("施工中");
                }else if(position == 2){
                    select_status_tv.setText("已竣工");
                }


            }
            dialog = DialogUtil.createProgressDialog(GjgxActivity.this,"提示","数据获取中..");
            obtainData();

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
